<?php
/**
 * ==============================================
 * Copy right 2015-2016 http://wei.meididi88.com
 * ----------------------------------------------
 * This is not a free software, without any authorization is not allowed to use and spread.
 * ==============================================
 * @author: hy
 * @date: 2015-9-10
 * @version: v1.0.0
 */
namespace Common\Model;

use Think\Model;

/**
 * 用户 Model模型类
 */
class UserModel extends Model{
	
	/**
	 * 清除用户绑定
	 * @param number $id
	 * @return boolean
	 */
	public function clearBind($id) {
		$userInfo = $this->getUserById($id);
		if (!$userInfo) {
			return true;	
		}
		
		$result = $this->where(array('id' => $id))->save(array('mobile' => 0));
		S('User_Phone_'.$userInfo ['mobile'], null);
		S('User_Id_'.$id, null);
		
		if ($result) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 用户绑定
	 * 
	 * @param array $data
	 * @return number|boolean
	 */
	public function userBind($data) {
		$autoIdModel = D('AutoId');
		
		$userId = $autoIdModel->createAutoId(array('type' => 'user'));
		$data ['id'] = $userId;
		
		if (!$this->create($data, 1)) {
			return false;
		} else {
			$result = $this->add();
			if ($result) {
				return $userId;
			} else return false;
		}
	}
	
	/**
	 * 通过手机号查询用户信息
	 * 
	 * @param string $mobile	手机号
	 * @return boolean|array
	 */
	public function getUserByMobile($mobile, $openid) {
// 		$cacheKey = 'User_Phone_'.$mobile.'_'.$openid;
// 		$userInfo = S($cacheKey);
// 		if (!$userInfo) {
			$userInfo = $this->where(array(
				'mobile' => $mobile,
				'open_id' => $openid
			))->find();
			
// 			if ($userInfo) {
// 				S($cacheKey, $userInfo);
// 			}
// 		}
		return $userInfo;
	}
	
	/**
	 * 通过用户编号获取用户信息
	 * 
	 * @param number $id	用户编号
	 * @return boolean|array
	 */
	public function getUserById($id) {
// 		$cacheKey = 'User_Id_'.$id;
// 		$userInfo = S($cacheKey);
		
// 		if (!$userInfo) {
			$userInfo = $this->where(array(
				'id' => $id
			))->find();
				
// 			if ($userInfo) {
// 				S($cacheKey, $userInfo);
// 			}
// 		}
		
		return $userInfo;
	}
	
	/**
	 * 根据用户openid获取用户信息
	 * @param string $openid
	 * 
	 * @return boolean|array
	 */
	public function getUserByOpenId($openid) {
		return M('user_weixin_relationship')
			->field(array('u.*'))
			->join('uw LEFT JOIN '.C('DB_PREFIX').'user as u on uw.user_id=u.id')
			->where(array('uw.open_id' => $openid, 'uw.status' => 1))
			->find();
	}
	
	public function hasBindRecord($openid) {
		$relationShip = M('user_weixin_relationship')->where(array('open_id' => $openid))->find();
		
		if (!$relationShip) {
			return false;
		}
		
		return $this->where(array('id' => $relationShip ['user_id']))->find();
	}
	
	public function clearMobileCache($mobile, $openid) {
		S('User_Phone_'.$mobile.'_'.$openid, null);
	}
	
	public function clearCache($id) {
		S('User_Id_'.$id, null);
	}
	
	/**
	 * 更新用户设置地区
	 * 
	 * @param string $openid
	 * @param array $cityConfig
	 */
	public function updateCitySetting($openid, $cityConfig) {
		$user = $this->getUserByOpenId($openid);
		$this->where(array('open_id' => $openid))->save($cityConfig);
		
		S('User_Phone_'.$user['mobile'].'_'.$openid, null);
		S('User_Id_'.$user['id'], null);
		S('User_Opendid_'.$openid, null);
		return true;
	}
}

?>